//
//  EmoticonPackage.swift
//  表情键盘
//
//  Created by 孙明海 on 16/2/17.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import UIKit
/// 表情包模型
class EmoticonPackage: NSObject {
    /// id	目录名
    var id: String?
    /// group_name_cn	表情分组名称- 显示在 toolbar 上
    var group_name_cn: String?
    /// emoticons	 数组
    var emoticons = [Emoticon]()
    
    
    
    init(dict: [String: AnyObject]) {
        super.init()
        id = dict["id"] as? String
        group_name_cn = dict["group_name_cn"] as? String
        var index = 0
        if let array = dict["emoticons"] as? [[String: AnyObject]] {
            
            for var dict in array {
                if let png = dict["png"] as? String,dir = id {
                    dict["png"] = dir + "/" + png
                }
                emoticons.append(Emoticon(dict: dict))
                
                index++
                if index == 20 {
                    emoticons.append(Emoticon(isRemoved: true))
                    index = 0
                }
            }
        }
        appendEmptyEmoticon()
    }
    
    private func appendEmptyEmoticon() {
        let count = emoticons.count % 21
        if emoticons.count > 0 && count == 0 {
            return
        }
        for _ in count..<20 {
            emoticons.append(Emoticon(isEmpty: true))
        }
        emoticons.append(Emoticon(isRemoved: true))
    }

    override var description: String {
        let keys = ["id", "group_name_cn", "emoticons"]
        return dictionaryWithValuesForKeys(keys).description
    }
}
